package com.wsh.crawler.utils;

import cn.hutool.extra.spring.SpringUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.stereotype.Component;

import java.util.Locale;

@Component
public class MessageUtil {

    /**
     * 使用方式：key传入messages.properties定义好的值即可
     * */
    public static String getMessage(String key) {
        ApplicationContext context = SpringUtil.getApplicationContext();
        ResourceBundleMessageSource messageSource = context.getBean(ResourceBundleMessageSource.class);

        Locale locale = LocaleContextHolder.getLocale();
        return messageSource.getMessage(key, null, locale);
    }
}
